package org.hrqing.authorization;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.junit.jupiter.api.Test;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther Hrqing
 */
public class OAuthTokenExchange {

    // http://localhost:38100/oauth/authorize?response_type=code&client_id=system&scope=read&redirect_uri=http://www.baidu.com

    @Test
    void codeTokenTest() {
        // 授权服务器的 Token 端点
        String tokenEndpoint = "http://localhost:38100/oauth/token";

        // 请求参数
        String clientId = "system";
        String clientSecret = "6h87RixqJiTY5edeuh6p";
        String redirectUri = "http://www.baidu.com";
        String authorizationCode = "0789e8d09cc0";

        // 构造请求体
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("grant_type", "authorization_code");
        body.add("code", authorizationCode);
        body.add("redirect_uri", redirectUri);

        // 构造请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setBasicAuth(clientId, clientSecret); // 使用 Basic Auth 传递客户端凭据

        // 构造 HTTP 请求
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers);

        // 发送请求
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<String> response = restTemplate.exchange(
                    tokenEndpoint,
                    HttpMethod.POST,
                    request,
                    String.class
            );

            // 打印 Token 响应
            System.out.println("Access Token Response: " + response.getBody());
        } catch (Exception e) {
            throw e;
        }
    }

    @Test
    void clientTokenTest() {
        // 授权服务器的 Token 端点
        String tokenEndpoint = "http://localhost:38100/oauth/token";

        // 请求参数
        String clientId = "system";
        String clientSecret = "6h87RixqJiTY5edeuh6p";

        // 构造请求体
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("grant_type", "client_credentials");

        // 构造请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setBasicAuth(clientId, clientSecret);

        // 构造 HTTP 请求
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers);

        // 发送请求
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<String> response = restTemplate.exchange(
                    tokenEndpoint,
                    HttpMethod.POST,
                    request,
                    String.class
            );

            // 打印 Token 响应
            System.out.println("Access Token Response: " + response.getBody());
        } catch (Exception e) {
            throw e;
        }
    }

    @Test
    void introspectTest() {
        // 授权服务器的 Token 端点
        String tokenEndpoint = "http://localhost:38100/oauth/introspect";

        // 请求参数
        String clientId = "system";
        String clientSecret = "6h87RixqJiTY5edeuh6p";

        // 构造请求体
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("token", "06220f587f2f4468bf7e5160067a1aae");

        // 构造请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setBasicAuth(clientId, clientSecret); // 使用 Basic Auth 传递客户端凭据

        // 构造 HTTP 请求
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers);

        // 发送请求
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<String> response = restTemplate.exchange(
                    tokenEndpoint,
                    HttpMethod.POST,
                    request,
                    String.class
            );

            // 打印 Token 响应
            System.out.println("Access Token Response: " + response.getBody());
        } catch (Exception e) {
            throw e;
        }
    }

    @Test
    void refreshTest() {
        // 授权服务器的 Token 端点
        String tokenEndpoint = "http://localhost:38100/oauth/token";

        // 请求参数
        String clientId = "client-id";
        String clientSecret = "client-secret";

        // 构造请求体
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("grant_type", "refresh_token");
        body.add("refresh_token", "0caacd0e7ced48a8aad100bda0b6d2dd");

        // 构造请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setBasicAuth(clientId, clientSecret); // 使用 Basic Auth 传递客户端凭据

        // 构造 HTTP 请求
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers);

        // 发送请求
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<String> response = restTemplate.exchange(
                    tokenEndpoint,
                    HttpMethod.POST,
                    request,
                    String.class
            );

            // 打印 Token 响应
            System.out.println("Access Token Response: " + response.getBody());
        } catch (Exception e) {
            throw e;
        }
    }

    @Test
    void userinfoTest() {
        // 授权服务器的 userinfo 端点
        String tokenEndpoint = "http://localhost:38100/api/userinfo";

        // 构造请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth("30cf682b0c354bcc9a68a7209a6174fe");

        // 构造 HTTP 请求
        HttpEntity<?> request = new HttpEntity<>(headers);

        // 发送请求
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<String> response = restTemplate.exchange(
                    tokenEndpoint,
                    HttpMethod.GET,
                    request,
                    String.class
            );

            // 打印 Token 响应
            System.out.println("User Response: " + response.getBody());
        } catch (Exception e) {
            throw e;
        }
    }
}
